/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Control;

import Control.exceptions.NonexistentEntityException;
import Control.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entidades.Direccion;
import Entidades.Factura;
import Entidades.ResponsablePago;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author USUARIO
 */
public class ResponsablePagoJpaController implements Serializable {

    public ResponsablePagoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ResponsablePago responsablePago) throws PreexistingEntityException, Exception {
        if (responsablePago.getFacturaList() == null) {
            responsablePago.setFacturaList(new ArrayList<Factura>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Direccion direccion = responsablePago.getDireccion();
            if (direccion != null) {
                direccion = em.getReference(direccion.getClass(), direccion.getIdDireccion());
                responsablePago.setDireccion(direccion);
            }
            List<Factura> attachedFacturaList = new ArrayList<Factura>();
            for (Factura facturaListFacturaToAttach : responsablePago.getFacturaList()) {
                facturaListFacturaToAttach = em.getReference(facturaListFacturaToAttach.getClass(), facturaListFacturaToAttach.getNroFactura());
                attachedFacturaList.add(facturaListFacturaToAttach);
            }
            responsablePago.setFacturaList(attachedFacturaList);
            em.persist(responsablePago);
            if (direccion != null) {
                direccion.getResponsablePagoList().add(responsablePago);
                direccion = em.merge(direccion);
            }
            for (Factura facturaListFactura : responsablePago.getFacturaList()) {
                ResponsablePago oldResponsablePagoOfFacturaListFactura = facturaListFactura.getResponsablePago();
                facturaListFactura.setResponsablePago(responsablePago);
                facturaListFactura = em.merge(facturaListFactura);
                if (oldResponsablePagoOfFacturaListFactura != null) {
                    oldResponsablePagoOfFacturaListFactura.getFacturaList().remove(facturaListFactura);
                    oldResponsablePagoOfFacturaListFactura = em.merge(oldResponsablePagoOfFacturaListFactura);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findResponsablePago(responsablePago.getCuit()) != null) {
                throw new PreexistingEntityException("ResponsablePago " + responsablePago + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ResponsablePago responsablePago) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ResponsablePago persistentResponsablePago = em.find(ResponsablePago.class, responsablePago.getCuit());
            Direccion direccionOld = persistentResponsablePago.getDireccion();
            Direccion direccionNew = responsablePago.getDireccion();
            List<Factura> facturaListOld = persistentResponsablePago.getFacturaList();
            List<Factura> facturaListNew = responsablePago.getFacturaList();
            if (direccionNew != null) {
                direccionNew = em.getReference(direccionNew.getClass(), direccionNew.getIdDireccion());
                responsablePago.setDireccion(direccionNew);
            }
            List<Factura> attachedFacturaListNew = new ArrayList<Factura>();
            for (Factura facturaListNewFacturaToAttach : facturaListNew) {
                facturaListNewFacturaToAttach = em.getReference(facturaListNewFacturaToAttach.getClass(), facturaListNewFacturaToAttach.getNroFactura());
                attachedFacturaListNew.add(facturaListNewFacturaToAttach);
            }
            facturaListNew = attachedFacturaListNew;
            responsablePago.setFacturaList(facturaListNew);
            responsablePago = em.merge(responsablePago);
            if (direccionOld != null && !direccionOld.equals(direccionNew)) {
                direccionOld.getResponsablePagoList().remove(responsablePago);
                direccionOld = em.merge(direccionOld);
            }
            if (direccionNew != null && !direccionNew.equals(direccionOld)) {
                direccionNew.getResponsablePagoList().add(responsablePago);
                direccionNew = em.merge(direccionNew);
            }
            for (Factura facturaListOldFactura : facturaListOld) {
                if (!facturaListNew.contains(facturaListOldFactura)) {
                    facturaListOldFactura.setResponsablePago(null);
                    facturaListOldFactura = em.merge(facturaListOldFactura);
                }
            }
            for (Factura facturaListNewFactura : facturaListNew) {
                if (!facturaListOld.contains(facturaListNewFactura)) {
                    ResponsablePago oldResponsablePagoOfFacturaListNewFactura = facturaListNewFactura.getResponsablePago();
                    facturaListNewFactura.setResponsablePago(responsablePago);
                    facturaListNewFactura = em.merge(facturaListNewFactura);
                    if (oldResponsablePagoOfFacturaListNewFactura != null && !oldResponsablePagoOfFacturaListNewFactura.equals(responsablePago)) {
                        oldResponsablePagoOfFacturaListNewFactura.getFacturaList().remove(facturaListNewFactura);
                        oldResponsablePagoOfFacturaListNewFactura = em.merge(oldResponsablePagoOfFacturaListNewFactura);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = responsablePago.getCuit();
                if (findResponsablePago(id) == null) {
                    throw new NonexistentEntityException("The responsablePago with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ResponsablePago responsablePago;
            try {
                responsablePago = em.getReference(ResponsablePago.class, id);
                responsablePago.getCuit();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The responsablePago with id " + id + " no longer exists.", enfe);
            }
            Direccion direccion = responsablePago.getDireccion();
            if (direccion != null) {
                direccion.getResponsablePagoList().remove(responsablePago);
                direccion = em.merge(direccion);
            }
            List<Factura> facturaList = responsablePago.getFacturaList();
            for (Factura facturaListFactura : facturaList) {
                facturaListFactura.setResponsablePago(null);
                facturaListFactura = em.merge(facturaListFactura);
            }
            em.remove(responsablePago);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ResponsablePago> findResponsablePagoEntities() {
        return findResponsablePagoEntities(true, -1, -1);
    }

    public List<ResponsablePago> findResponsablePagoEntities(int maxResults, int firstResult) {
        return findResponsablePagoEntities(false, maxResults, firstResult);
    }

    private List<ResponsablePago> findResponsablePagoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ResponsablePago.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ResponsablePago findResponsablePago(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ResponsablePago.class, id);
        } finally {
            em.close();
        }
    }

    public int getResponsablePagoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ResponsablePago> rt = cq.from(ResponsablePago.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
